#include "coreservice.h"

using namespace TA;

static ushort ChineseCharBegin = 0x4e00;
static ushort ChineseCharEnd   = 0x9FFF;

CoreService::CoreService(QObject *parent) : QObject(parent)
{

}

Project *CoreService::createProject(QObject* parent,const QString &title)
{
    const QString uuid = QUuid::createUuid().toString();
    if(parent == nullptr) parent = this;

    Project* project = new Project(parent,uuid);

    project->setProjectTitle(title);

    return project;
}

Document* CoreService::appendDocument(Project *p, const QString &docTitle, const QString& content)
{
    const QString uuid = QUuid::createUuid().toString();
    Document* doc = new Document(p,uuid);
    doc->setCreateDate(QDate::currentDate());

    doc->setTitle(docTitle);
    doc->setContent(content);


    if(!p->hasDocument(doc)){
        p->getDocuments()->push_back(doc);
    }else{
        delete doc;
        doc = nullptr;
    }

    return doc;

}

StatementRawPtrArray CoreService::discriminateDoc(Document *doc,const QString& sep)
{
    StatementRawPtrArray result;

    qDebug()<<"content:"<<doc->getContent();

    const QString& content = doc->getContent();

    int currentPos = 0;
    Statement* statement = nullptr;
    QString sb;

    QList<QChar> spes;

    if(!sep.isEmpty()){
        for(int i=0;i<sep.length();++i){
            spes.push_back(sep.at(i));
        }
    }

    while(currentPos< content.length()){
        const QChar c = content.at(currentPos);

        if(acceptChar(c,spes)){
            WordType type = codeType(c);

            if(statement == nullptr|| statement->getType()!=type){
                if(statement != nullptr && !sb.isEmpty()){
                    statement->setText(sb);
                    result.push_back(statement);
                }

                statement = new Statement(doc);
                statement->setDocument(doc);
                statement->setBeginPos(currentPos);
                statement->setType(type);

                sb.clear();
            }
            sb+=c;

        }else{
            if(statement!=nullptr && !sb.isEmpty() ){
                statement->setText(sb);
                result.push_back(statement);

                statement = nullptr;
                sb.clear();
            }
        }

        currentPos++;

        if(currentPos>=content.length() ){
            if (statement != nullptr && statement->getText().isEmpty()){
                statement->setText(sb);
                result.push_back(statement);
                statement = nullptr;
                sb.clear();
            }
        }

    }



    return result;
}

bool CoreService::isChineseCode(const QChar c)
{
    return c.unicode() >= ChineseCharBegin && c.unicode() <= ChineseCharEnd;
}

bool CoreService::isAlphabetic(const QChar c)
{
    return c.isLetter();
}

bool CoreService::isDigit(const QChar c)
{
    return c.isDigit();
}

bool CoreService::acceptChar(const QChar c, const QList<QChar>& seps)
{
    if(seps.indexOf(c)>0){
        return false;
    }

    WordType type = codeType(c);

    if(type == UNKNOW)
        return false;

    return true;
}

WordType CoreService::codeType(const QChar c)
{
    if(isChineseCode(c)){
        return CHINESE;
    }else if(isAlphabetic(c)){
        return LATIN;
    }else if(isDigit(c)){
        return NUMBER;
    }
    return UNKNOW;
}

WordRawPtrArray CoreService::analyzeStatements(const StatementRawPtrArray &statements, Document *doc) {
    WordRawPtrArray result;


    return result;
}
